<template>
  <el-dialog v-model="dialog.visible"
             :title="dialog.title"
             :close-on-click-modal="false"
             width="70%"
             :before-close="close">
    <avue-form v-if="actDetailOption" :option="actDetailOption" v-model="formModel"></avue-form>
    <el-divider>当前任务</el-divider>
    <el-form ref="formEdit" :model="formEditModel" :rules="formEditModelRules">
      <el-form-item label="流程任务" prop="targetTaskDefKey">
        <el-select v-model="formEditModel.targetTaskDefKey">
          <el-option v-for="(taskDef) in procTaskDefs" :label="taskDef.taskDefName" :value="taskDef.taskDefKey">
            <span style="float: left">{{ taskDef.taskDefName }}</span>
            <span v-if="taskDef.isCurrentTask"
                  style="float: right;color: var(--el-text-color-secondary);  font-size: 13px">
              <el-button link type="primary">当前任务</el-button>
            </span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="办理人">
        <start-tag-select-user :props="{value:'userId'}"
                               :multiple="false"
                               v-model="taskHandlerUsers"></start-tag-select-user>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right">
      <el-button @click="close">取消</el-button>
      <el-button @click="submit" type="primary">保 存</el-button>
    </div>
  </el-dialog>
</template>
<script>
import actBusinessOption from "@/option/start/activiti/actBusinessOption";
import {deepClone} from "utils/util";
import actTaskApi from "@/api/start/activiti/actTaskApi";
import StartTagSelectUser from "components/start-components/startSelectUser/startTagSelectUser.vue";

export default {
  name: "editBusinessDialog",
  components: {StartTagSelectUser},
  computed: {
    formEditModelRules() {
      return {
        targetTaskDefKey: [{required: true, message: "请选择流程任务"}],
        taskHandlerUsers: [{required: true, type: 'array', message: "请选择流程办理人"}],
      }
    }
  },
  data() {
    return {
      dialog: {
        visible: false,
        title: '',
      },
      actDetailOption: null,
      taskHandlerUsers: [],
      formModel: {},
      procTaskDefs: [],
      formEditModel: {},
      currentTaskInstId: null,

    }
  },
  methods: {
    show(businessEntity) {
      let actDetailOption = actBusinessOption()
      actDetailOption.menuBtn = false
      actDetailOption.boxType = "view"
      actDetailOption.detail = true;
      this.actDetailOption = actDetailOption
      this.currentTaskInstId = businessEntity.curTaskInstId;
      actTaskApi.getRunTaskDetailByTaskId(this.currentTaskInstId).then(res => {
        let data = res.data.data;
        let businessName = businessEntity.businessName
        if (!businessName || businessName.length < 1) {
          businessName = "【" + businessEntity.procStartUserFinalName + "】发起的【" + businessEntity.procDefName + "】流程";
        }
        let taskInstDetail = data.taskInstDetail || {};

        let procTaskDefs = data.procTaskDefs || []
        let procActivateTaskDefKey = taskInstDetail.taskDefKey || null;

        for (const taskDef of procTaskDefs) {
          if (taskDef.taskDefKey === procActivateTaskDefKey) {
            taskDef.isCurrentTask = true;
          }
        }
        this.formEditModel.targetTaskDefKey = procActivateTaskDefKey
        this.procTaskDefs = procTaskDefs;

        this.dialog.title = businessName;
        this.formModel = deepClone(businessEntity)
        this.dialog.visible = true;
      })

    },
    submit() {
      this.$refs.formEdit.validate().then(res => {
        if (!res) {
          return Promise.reject()
        }
        let formEditModel = deepClone(this.formEditModel);
        formEditModel.currentTaskInstId = this.currentTaskInstId
        formEditModel.taskHandlerUser = this.taskHandlerUsers[0]
        return actTaskApi.updateRunTask(formEditModel)
      }).then(res => {
        this.$message.success("操作成功")
        this.$emit("submit", this.close)
      }).catch((err) => {

      })
    },
    close() {
      this.dialog.visible = false;
      this.actDetailOption = null;
      this.formModel = {};
      this.procTaskDefs = []
      this.formEditModel = {}
      this.taskHandlerUsers = []
    },
  }
}
</script>
<style scoped>

</style>